package chapter21.chapter21_9;//: concurrency/MapComparisons.java
// {Args: 1 10 10} (Fast verification check during build)
// Rough comparison of thread-safe Map performance.

import java.util.concurrent.*;
import java.util.*;

import net.mindview.util.*;

abstract class MapTest
        extends Tester<Map<Integer, Integer>> {
    MapTest(String testId, int nReaders, int nWriters) {
        super(testId, nReaders, nWriters);
    }

    class Reader extends TestTask {
        long result = 0;

        void test() {
            for (long i = 0; i < testCycles; i++)
                for (int index = 0; index < containerSize; index++)
                    result += testContainer.get(index);
        }

        void putResults() {
            readResult += result;
            readTime += duration;
        }
    }

    class Writer extends TestTask {
        void test() {
            for (long i = 0; i < testCycles; i++)
                for (int index = 0; index < containerSize; index++)
                    testContainer.put(index, writeData[index]);
        }

        void putResults() {
            writeTime += duration;
        }
    }

    void startReadersAndWriters() {
        for (int i = 0; i < nReaders; i++)
            exec.execute(new Reader());
        for (int i = 0; i < nWriters; i++)
            exec.execute(new Writer());
    }
}

class SynchronizedHashMapTest extends MapTest {
    Map<Integer, Integer> containerInitializer() {
        return Collections.synchronizedMap(
                new HashMap<Integer, Integer>(
                        MapData.map(
                                new CountingGenerator.Integer(),
                                new CountingGenerator.Integer(),
                                containerSize)));
    }

    SynchronizedHashMapTest(int nReaders, int nWriters) {
        super("Synched HashMap", nReaders, nWriters);
    }
}

class ConcurrentHashMapTest extends MapTest {
    Map<Integer, Integer> containerInitializer() {
        return new ConcurrentHashMap<Integer, Integer>(
                MapData.map(
                        new CountingGenerator.Integer(),
                        new CountingGenerator.Integer(), containerSize));
    }

    ConcurrentHashMapTest(int nReaders, int nWriters) {
        super("ConcurrentHashMap", nReaders, nWriters);
    }
}

public class MapComparisons {
    public static void main(String[] args) {
        Tester.initMain(args);
        new SynchronizedHashMapTest(10, 0);
        new SynchronizedHashMapTest(9, 1);
        new SynchronizedHashMapTest(5, 5);
        new ConcurrentHashMapTest(10, 0);
        new ConcurrentHashMapTest(9, 1);
        new ConcurrentHashMapTest(5, 5);
        Tester.exec.shutdown();
    }
} /* Output: (Sample)
Type                             Read time     Write time
Synched HashMap 10r 0w        306052025049              0
Synched HashMap 9r 1w         428319156207    47697347568
readTime + writeTime =        476016503775
Synched HashMap 5r 5w         243956877760   244012003202
readTime + writeTime =        487968880962
ConcurrentHashMap 10r 0w       23352654318              0
ConcurrentHashMap 9r 1w        18833089400     1541853224
readTime + writeTime =         20374942624
ConcurrentHashMap 5r 5w        12037625732    11850489099
readTime + writeTime =         23888114831
JDK 1.8

Type                             Read time     Write time
Synched HashMap 10r 0w          6689836815              0
Synched HashMap 10r 0w          5847493628              0
Synched HashMap 10r 0w          6158237116              0
Synched HashMap 10r 0w          6169483076              0
Synched HashMap 10r 0w          6273794925              0
Synched HashMap 10r 0w          5462006294              0
Synched HashMap 10r 0w          6203335557              0
Synched HashMap 10r 0w          6214291577              0
Synched HashMap 10r 0w          6303880795              0
Synched HashMap 10r 0w          6046786319              0
Synched HashMap 9r 1w           5681090813      653772600
readTime + writeTime =          6334863413
Synched HashMap 9r 1w           5916255706      695189100
readTime + writeTime =          6611444806
Synched HashMap 9r 1w           6065935410      705111955
readTime + writeTime =          6771047365
Synched HashMap 9r 1w           5842335916      691463112
readTime + writeTime =          6533799028
Synched HashMap 9r 1w           5897917416      690359813
readTime + writeTime =          6588277229
Synched HashMap 9r 1w           6166478079      702668018
readTime + writeTime =          6869146097
Synched HashMap 9r 1w           5987005440      689664905
readTime + writeTime =          6676670345
Synched HashMap 9r 1w           5823880022      672116026
readTime + writeTime =          6495996048
Synched HashMap 9r 1w           5877738577      682343357
readTime + writeTime =          6560081934
Synched HashMap 9r 1w           5927901077      680518209
readTime + writeTime =          6608419286
Synched HashMap 5r 5w           3224771643     3959461436
readTime + writeTime =          7184233079
Synched HashMap 5r 5w           3223542617     3799564668
readTime + writeTime =          7023107285
Synched HashMap 5r 5w           3444672944     3751384257
readTime + writeTime =          7196057201
Synched HashMap 5r 5w           3386965241     3809530284
readTime + writeTime =          7196495525
Synched HashMap 5r 5w           3311758694     3760292333
readTime + writeTime =          7072051027
Synched HashMap 5r 5w           3432649116     3688060416
readTime + writeTime =          7120709532
Synched HashMap 5r 5w           3207224046     3804718102
readTime + writeTime =          7011942148
Synched HashMap 5r 5w           3337761501     3840446622
readTime + writeTime =          7178208123
Synched HashMap 5r 5w           3195024888     3958045535
readTime + writeTime =          7153070423
Synched HashMap 5r 5w           3208630540     3937401429
readTime + writeTime =          7146031969
ConcurrentHashMap 10r 0w         588082227              0
ConcurrentHashMap 10r 0w         316089785              0
ConcurrentHashMap 10r 0w         326090469              0
ConcurrentHashMap 10r 0w         314609741              0
ConcurrentHashMap 10r 0w         293035099              0
ConcurrentHashMap 10r 0w         335100322              0
ConcurrentHashMap 10r 0w         345739468              0
ConcurrentHashMap 10r 0w         279020201              0
ConcurrentHashMap 10r 0w         370299432              0
ConcurrentHashMap 10r 0w         503406969              0
ConcurrentHashMap 9r 1w          218917741       40809257
readTime + writeTime =           259726998
ConcurrentHashMap 9r 1w          287733702       33648929
readTime + writeTime =           321382631
ConcurrentHashMap 9r 1w          260646840       40819093
readTime + writeTime =           301465933
ConcurrentHashMap 9r 1w          238988812       28205983
readTime + writeTime =           267194795
ConcurrentHashMap 9r 1w          248122253       37171362
readTime + writeTime =           285293615
ConcurrentHashMap 9r 1w          242986775       33867878
readTime + writeTime =           276854653
ConcurrentHashMap 9r 1w          266204391       47320863
readTime + writeTime =           313525254
ConcurrentHashMap 9r 1w          251557877       43613691
readTime + writeTime =           295171568
ConcurrentHashMap 9r 1w          173342914       38765587
readTime + writeTime =           212108501
ConcurrentHashMap 9r 1w          258565968       66495185
readTime + writeTime =           325061153
ConcurrentHashMap 5r 5w          144226066      658544585
readTime + writeTime =           802770651
ConcurrentHashMap 5r 5w          248296300      636428986
readTime + writeTime =           884725286
ConcurrentHashMap 5r 5w          235723389      656585587
readTime + writeTime =           892308976
ConcurrentHashMap 5r 5w          241103472      656597987
readTime + writeTime =           897701459
ConcurrentHashMap 5r 5w          156509897      595185679
readTime + writeTime =           751695576
ConcurrentHashMap 5r 5w          254763860      641742786
readTime + writeTime =           896506646
ConcurrentHashMap 5r 5w          257337372      584490944
readTime + writeTime =           841828316
ConcurrentHashMap 5r 5w          220399924      595318674
readTime + writeTime =           815718598
ConcurrentHashMap 5r 5w          308347446      637672122
readTime + writeTime =           946019568
ConcurrentHashMap 5r 5w          164874017      549023282
readTime + writeTime =           713897299
*///:~
